/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pneuservis.frontend.web;

import java.util.List;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pneuservis.backend.services.ServicePneuServiceLocal;
import pneuservis.backend.to.ServicePneuTO;

/**
 *
 * @author jajaja
 */
@UrlBinding("/services/{$event}/{service.id}")
public class ServicesActionBean implements ActionBean{

    final static Logger log = LoggerFactory.getLogger(ServicesActionBean.class);
    
    private ActionBeanContext context;
    
    @ValidateNestedProperties(value = {
        @Validate(on = {"add", "save"}, field = "name", required = true),
        @Validate(on = {"add", "save"}, field = "price", required = true, minvalue = 0),
    })
    private ServicePneuTO service;
    
    @EJB(lookup="java:global/cz.muni.fi.pa165.pneuservis_Pneuservis-ear_ear_1.0-SNAPSHOT/Pneuservis-ejb-1.0-SNAPSHOT/ServicePneuService")
    private ServicePneuServiceLocal servicePneuService;
    
    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }

    public ServicePneuTO getService() {
        return service;
    }

    public void setService(ServicePneuTO service) {
        this.service = service;
    }
    
    public List<ServicePneuTO> getServices() {
        return servicePneuService.getAllServices();
    }
    
    @DefaultHandler
    public Resolution all() {
        log.debug("all()");
        return new ForwardResolution("/showService.jsp");
    }
    
    public Resolution add() {
        log.debug("add() service={}", service);
        servicePneuService.createService(service);
        return new RedirectResolution(this.getClass(), "all");
    }
    
    public Resolution edit() {
        log.debug("edit() service={}", service);
        return new ForwardResolution("/editService.jsp");
    }
    
    public Resolution save() {
        log.debug("save() service={}", service);
        try {
            servicePneuService.updateService(service);
        } catch (EJBException ex) {
            log.warn(ex.getMessage());
        }
        return new RedirectResolution(this.getClass(), "all");
    }
    
    public Resolution delete() {
        log.debug("delete() service()", service);
        try {
            servicePneuService.removeService(service);
        } catch (EJBException ex) {
            log.warn(ex.getMessage());
        }
        return new RedirectResolution(this.getClass(), "all");
    }
    
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "save"})
    public void loadServiceFromDatabase() {
        String ids = context.getRequest().getParameter("service.id");
        if (ids == null) {
            return;
        }
        service = servicePneuService.getService(Long.parseLong(ids));
    }
}